﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Frame.Lib.Core.Module;
using Cms.BLL.Menu;
using Frame.Lib.Utilities;
using Cms.Entity.Menu;


namespace Cms.Admin.Menu
{
    public partial class TransModule : PageSelectTree
    {
        private ModuleBO Module
        {
            get
            {
                return ModuleBO.Instance<ModuleBO>();
            }
        }
        private MenuBO Menu
        {
            get
            {
                return MenuBO.Instance<MenuBO>();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
            }

            tvModule.Attributes.Add("onclick", "SingleSelect()");
        }

        private void BindTree()
        {
            Module.BindTreeView(tvModule, true, null);

            tvModule.ExpandAll();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckedIDs.Trim() == "")
            {
                JSUtil.Alert("请选择上级模块");
                return;
            }

            int upperID = StrUtil.SplitToInt(CheckedIDs).First();

            IEnumerable<int> ids = StrUtil.SplitToInt(Request.QueryString["ids"]);

            List<MenuInfo> menuInfos = Menu.GetList(ids);
            ModuleInfo upperModule = Module.GetInfo(upperID);           

            foreach (MenuInfo info in menuInfos)
            {
                ModuleInfo moduleInfo = new ModuleInfo();
                moduleInfo.Name = info.Name;
                moduleInfo.Code = "";
                moduleInfo.Depth = upperModule.Depth + 1;
                moduleInfo.IconUrl = "common/images/module/ico_031.gif";
                moduleInfo.NavUrl = "SiteAdmin/Article/list.aspx?menucode=" + info.Code;
                moduleInfo.Orders = this.Module.GetMaxOrder();
                moduleInfo.Path = upperModule.Path + upperModule.ID + ",";
                moduleInfo.UpperID = upperModule.ID;
                this.Module.Save(moduleInfo);               
            }


            string strScript = "alert('转换成功');window.close();returnValue=true";
            JSUtil.ResponseJS(strScript);
        }
    }
}